local skel = fk.CreateSkill {
  name = "rmt__qiejiang",
  tags = {Skill.Combo},
}

Fk:loadTranslationTable{
  ["rmt__qiejiang"] = "切江",
  [":rmt__qiejiang"] = "连招技（伤害牌+与上张牌颜色相同牌），此牌额外结算一次，上述每有一张牌造成伤害，你摸一张牌。",

  ["@rmt__qiejiang"] = "切江",

  ["$rmt__qiejiang1"] = "彼中流置军，当驭沧浪吞之！",
  ["$rmt__qiejiang2"] = "星夜大江渡，破晓凯歌旋！",
}

skel:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and
      data.extra_data and data.extra_data.combo_skill and data.extra_data.combo_skill[skel.name]
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@rmt__qiejiang", 0)
    data.additionalEffect = (data.additionalEffect or 0) + 1
  end,
})

skel:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and
      data.extra_data and data.extra_data.combo_skill and data.extra_data.combo_skill[skel.name] then
      local n = data.damageDealt and 1 or 0
      player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        if e.id < player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true).id then
          local use = e.data
          if use.from == player then
            if use.damageDealt then
              n = n + 1
            end
            return true
          end
        end
      end, 1)
      if n > 0 then
        event:setCostData(self, n)
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:drawCards(event:getCostData(self), skel.name)
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "@rmt__qiejiang", data.card:getColorString())
    if #player:getTableMark("@rmt__qiejiang") == 2 then
      local mark = player:getTableMark("@rmt__qiejiang")
      if mark[1] == mark[2] and data.card.color ~= Card.NoColor then
        data.extra_data = data.extra_data or {}
        data.extra_data.combo_skill = data.extra_data.combo_skill or {}
        data.extra_data.combo_skill[skel.name] = true
        return
      end
    end
    if data.card.is_damage_card then
      room:setPlayerMark(player, "@rmt__qiejiang", {data.card:getColorString()})
    else
      room:setPlayerMark(player, "@rmt__qiejiang", 0)
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@rmt__qiejiang", 0)
end)

return skel
